<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: yangweijie <yangweijiester@gmail.com> <code-tech.diandian.com>
// +----------------------------------------------------------------------
namespace Admin\Model;
use Think\Model;

/**
 * 插件模型
 * @author yangweijie <yangweijiester@gmail.com>
 */

class MenuModel extends Model {

    protected $_validate = array(
        array('title','require','标题必须填写'), 
        array('url','require','链接必须填写'), 
    );

    /* 自动完成规则 */
    protected $_auto = array(
        array('title', 'htmlspecialchars', self::MODEL_BOTH, 'function'),
        array('status', '1', self::MODEL_INSERT),
    );

    /**
     * 获取树状权限菜单列表
     */
    function getTreeMenus(){
        $menus  = $this
            ->order('pid,sort')
            ->getField('id, title, pid, sort, url, hide, tip, group, is_dev, status, icon');
       $Auth       =   new \Think\Auth(); 
        foreach($menus as &$menu){
            if($menu['hide'] == 0){
                $menu['hide_title']     = '显示';
            }else{
                $menu['hide_title']     = '隐藏';
            }

            //权限检测
            $rule   = strtolower('Admin/'.$menu['url']);
            $auth   = $Auth->check($rule, UID, array('in','1,2'), 'url');
            if($auth || IS_ROOT){
                $menu['is_auth']    = 1;
            }else{
                $menu['is_auth']    = 0;
            }
        }

        $menus[0]   = array();
        foreach($menus as &$menu){
            $menus[$menu['pid']]['child'][] = &$menu;
        }
        
        $menus  = $menus[0]['child'];
        return $menus;
    }
}
